#!/usr/bin/perl -w # for syntax highlighting
# CSS-V1

# Take all the args and update portal ... and then
# redirect to page in the LP parameter.
use gri_secmgr qw(get_cookie_value redirect_header);
use gri_user_alert_config;
use gri_database;

sub mm_form_docrepos_add_section_process {
	my $str="";

	my $db_dir=$::GRI_FRONTEND->get_global_directory("database");
	my $DB=new gri_database("$db_dir/gri.db");
	
	my $sec_name=$::PARAM{name};
	my $sec_parent=$::PARAM{parent};
	my $sec_title=$::PARAM{title};
	my $sec_description=$::PARAM{description};
	my $sec_readers=$::PARAM{readers};
	my $sec_publishers=$::PARAM{publishers};
	my $sec_attributes=$::PARAM{attributes};
	$sec_parent="" if !defined($sec_parent);
	$sec_readers="" if !defined($sec_readers);
	$sec_publishers="" if !defined($sec_publishers);
	$sec_attributes="" if !defined($sec_attributes);

	#################################################################
	# Check to ensure we do not already have a section of this	#
	# name and raise an error if we do.				#
	#################################################################
	{
		my ($x,$y)=$DB->get_section(section=>$sec_name);
		print STDERR "get_section=$x,$y\n";
		if($x==1) {
			my $serror="Error Adding New Section";
			my $error="Specified Document Repository Section already exists.";
			my $link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
			if(!exists($::PARAM{lp})) {
				# We need to encode the current data for repopulation into the form!
				my $s="";
				$s.="sec_name=".string_to_hex($sec_name);
				$s.="::sec_title=".string_to_hex($sec_title);
				$s.="::sec_description=".string_to_hex($sec_description);
				$s.="::sec_attributes=".string_to_hex($sec_attributes);
				$s.="::sec_parent=".string_to_hex($sec_parent);
				$s.="::sec_readers=".string_to_hex($sec_readers);
				$s.="::sec_publishers=".string_to_hex($sec_publishers);
				$link.=",previouspage=form_docrepos_admin_add_section::$s";
			}
			redirect_header(path => $link);
			exit;
		}
	}

	#################################################################################
	# Everything should be in place, so now add the section...			#
	#################################################################################

WRITERECORD: {
		$serror=$error=undef;
		my ($x,$y)=$DB->add_section(
			section=>$sec_name, parent=>$sec_parent, attributes=>$sec_attributes,
			description=>$sec_description, title=>$sec_title,
			);
		if($x!=1) {
			$serror="Error Adding New Section";
			$error="Unable to write to database - error RC was $x.";
			last WRITERECORD;
		}
		# Ok - if the basic record has been written we should be able to 
		# set the reader/publisher groups if any have been defined.

		for my $cparam (keys %::PARAM) {
			if($cparam =~ /^reader_(.*)/) {
				my $reader=$1;
				($x,$y)=$DB->add_group_to_reader_list(section=>$sec_name,group=>$reader);
				if($x!=1) {
					$serror="Error Adding group to Section Reader List";
					$error="An error occurred whilst attempting to add group '$reader' to the section '$sec_name' reader list - RC=$x";
					last WRITERECORD;
				}
			}
			if($cparam =~ /^publisher_(.*)/) {
				my $reader=$1;
				($x,$y)=$DB->add_group_to_publisher_list(section=>$sec_name,group=>$reader);
				if($x!=1) {
					$serror="Error Adding group to Section Publisher List";
					$error="An error occurred whilst attempting to add group '$reader' to the section '$sec_name' publisher list - RC=$x";
					last WRITERECORD;
				}
			}
		}
	}

	if(defined($serror)) {
		my $link=link_to_str("","","show_generic_error","?serror=".string_to_hex($serror).",error=".string_to_hex($error));
		if(!exists($::PARAM{lp})) {
			# We need to encode the current data for repopulation into the form!
			my $s="";
			$s.="sec_name=".string_to_hex($sec_name);
			$s.="::sec_title=".string_to_hex($sec_title);
			$s.="::sec_description=".string_to_hex($sec_description);
			$s.="::sec_attributes=".string_to_hex($sec_attributes);
			$s.="::sec_parent=".string_to_hex($sec_parent);
			$s.="::sec_readers=".string_to_hex($sec_readers);
			$s.="::sec_publishers=".string_to_hex($sec_publishers);
			$link.=",previouspage=form_docrepos_admin_add_section::$s";
		}
		redirect_header(path => $link);
		exit;
	}

	my $link=link_to_str("","","/admin_page","");
	redirect_header(path => $link);
	exit;
}

